//
//  AllBtnViewController.m
//  1243
//
//  Created by 我的 on 16/4/7.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "AllBtnViewController.h"

@interface AllBtn : UIButton

@end

@implementation AllBtn

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
//        [self setBackgroundImage:[UIImage imageNamed:@"btn_filter_normal"] forState:UIControlStateNormal];
//        [self setBackgroundImage:[UIImage imageNamed:@"btn_filter_selected"] forState:UIControlStateHighlighted];
        [self setBackgroundImage:[UIImage imageNamed:@"11_03"] forState:UIControlStateNormal];
        [self setBackgroundImage:[UIImage imageNamed:@"11_06"] forState:UIControlStateHighlighted];
        
        [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        [self setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    }
    return self;
}



@end

@interface AllBtnViewController ()

@property (nonatomic, strong) NSArray *array;

@end

@implementation AllBtnViewController

static AllBtnViewController *allBtnVC = nil;
+ (AllBtnViewController *)sharedAllBtnVC {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        allBtnVC = [[AllBtnViewController alloc] init];
        allBtnVC.view.backgroundColor = [UIColor lightGrayColor];
    });
    return allBtnVC;
}

- (NSArray *)array {
    
    if (!_array) {
        _array = @[@"全部", @"充值", @"提现", @"转账",@"提成", @"奖金",  @"收款",@"积分奖励",@"积分转盘"];
        
    }
    return _array;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //创建所有按钮
    [self setUpAllBtn];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self backAllBtnView];
}

- (void)loadAllBtnView {
    //1.拿到window
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    //2.设置view的frame，添加到window上
    self.view.frame = CGRectMake((window.width/3 - 81)/2, HIGHT, WIDTH, HEIGHT);
    self.view.backgroundColor = [UIColor clearColor];
//    self.view.frame = window.frame;
    
    [window addSubview:self.view];
    //3.view的y值从整个屏幕的高到0
//    self.view.height = 490;
    [UIView animateWithDuration:0.00000000001 animations:^{

        
        self.view.y = 104;
    } completion:^(BOOL finished) {
   
        
    }];
}

- (void)backAllBtnView {
    //1.拿到window
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [UIView animateWithDuration:0.01 animations:^{
        self.view.y = window.height;
//        [window willRemoveSubview:self.view];
        
    } completion:^(BOOL finished) {
        [self.view removeFromSuperview];
    }];
}

- (void)setUpAllBtn {
    
    //边界
    CGFloat inset = 0;
    //btn 宽高
    CGFloat btnW = 81;
    CGFloat btnH = 45;
    NSInteger count = self.array.count;
    
    
    UIView  *bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, btnW, 45 * 5)];
//    bottomView.layer.masksToBounds = YES;
//    bottomView.layer.cornerRadius=5;
    bottomView.layer.shadowColor=[UIColor grayColor].CGColor;
    bottomView.layer.shadowOffset=CGSizeMake(0.5, 0.5);
    bottomView.layer.shadowOpacity=100;
    bottomView.layer.shadowRadius=5;
    
    [self.view addSubview:bottomView];
    
    UIScrollView *backView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, btnW, 45 * 5)];
    backView.showsVerticalScrollIndicator = NO;
//    backView.layer.masksToBounds = YES;
//    backView.layer.cornerRadius = 5;
//    backView.layer.shadowOffset = CGSizeMake(0.5, 0.5);
//    backView.layer.shadowRadius = 5;
//    backView.layer.shadowColor = [[UIColor grayColor] CGColor];
//    
    [backView setContentSize:CGSizeMake(btnW, 45 * count)];
    
    [bottomView addSubview:backView];
    for (NSInteger i = 0; i < count; i++) {
        //创建btn
        AllBtn *btn = [[AllBtn alloc] initWithFrame:CGRectMake(0, i * (inset + btnH) + inset, btnW, btnH)];
        //传递模型
        [btn setTitle:self.array[i] forState:UIControlStateNormal];
        btn.tag = i;
        //点击触发方法
        [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        [backView addSubview:btn];
    }
    
    //设置控制器的size
//    self.preferredContentSize = CGSizeMake(inset * 2 + btnW, count * (inset + btnH) + inset);
}

- (void)clickBtn:(AllBtn *)btn {
    //发送通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"AllBtnNoti" object:nil userInfo:@{@"AllBtnTitle" : btn.titleLabel.text , @"AllBtnTag":[NSString stringWithFormat:@"%ld", (long)btn.tag]}];
//    btn.backgroundColor = [UIColor blueColor];
    [self backAllBtnView];
    
    NSLog(@"%@", btn.titleLabel.text);
  

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
